﻿using StoryTeller.Domain;
using StoryTeller.Domain.Exceptions;
using StoryTeller.Domain.Repository;
using StoryTeller.Infrastructure.DataAccess.DAL;
using StoryTeller.Infrastructure.DataAccess.DAL.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoryTeller.Repository
{
    public class ContributionRepository : BaseRepository<Contribution>, IContributionRepository
    {
        public IContributionDAL dal { get { return DALFactory.GetContributionDAL(); } }

        public void Insert(Contribution entidade)
        {
            try
            {
                if (entidade.Validate())
                    throw new ValidationMessagesException(entidade.ValidationMessages);

                dal.Insert(entidade);
            }
            catch (ValidationMessagesException ex) 
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.ContributionInsertError, ex);
            }
        }

        public void Update(Contribution entidade)
        {
            try
            {
                if (entidade.Validate())
                    throw new ValidationMessagesException(entidade.ValidationMessages);

                dal.Update(entidade);
            }
            catch (ValidationMessagesException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.ContributionUpdateError, ex);
            }
        }

        public void Delete(int id)
        {
            try
            {
                dal.Delete(id);
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.ContributionDeleteError, ex);
            }
        }

        public ICollection<Contribution> Select()
        {
            try
            {
                return dal.Select();
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.ContributionSelectAllError, ex);
            }
        }

        public Contribution Select(int id)
        {
            try
            {
                return dal.Select(id);
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.ContributionSelectByIdError, ex);
            }
        }
    }
}
